/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import dbhandler.DBConnector;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.*;
import java.io.*;
import java.net.*;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.regex.*;
import org.apache.commons.lang.WordUtils;

/**
 *
 * @author Tokino
 */
public class ChargeValidate {

    private Hashtable errors;
    private String dhousenumber;
    private String dstreet;
    private String dsubdivision;
    private String dcity;
    private String dpostal;
    private String dcountry;
    private String bhousenumber;
    private String bstreet;
    private String bsubdivision;
    private String bcity;
    private String bpostal;
    private String bcountry;
    private String firstName;
    private String lastName;
    private String middleInitial;
    private String creditcardnumber;
    private String creditcardtype;
    private String creditcardexpiration;
    private String username;
    private int accounttype;

    public ChargeValidate() {
        errors = new Hashtable();

        dhousenumber = "";
        dstreet = "";
        dsubdivision = "";
        dcity = "";
        dpostal = "";
        dcountry = "";
        bhousenumber = "";
        bstreet = "";
        bsubdivision = "";
        bcity = "";
        bpostal = "";
        bcountry = "";
        firstName = "";
        lastName = "";
        middleInitial = "";
        creditcardnumber = "";
        creditcardtype = "";
        creditcardexpiration = "";
        username = "";
        accounttype = 0;

    }

    public boolean checkcreditinfo() throws SQLException, ClassNotFoundException, NoSuchAlgorithmException {

        boolean bool = true;
        DBConnector dbConnectorchk = null;
        dbConnectorchk = new DBConnector("localhost", 3306, "selector", "53l3ct0r", "bookaholic");

        if (dhousenumber.equals("") || !dhousenumber.matches("(?=^.{1,}$)(?=[0-9])(?!.*[\\W\\x00-\\x1F]).*$")) {
            errors.put("dhousenumber", "Please enter a valid house number");
            errors.put("status", "Failure");
            dhousenumber = "";
            bool = false;
        }

        if (bhousenumber.equals("") || !bhousenumber.matches("(?=^.{1,}$)(?=[0-9])(?!.*[\\W\\x00-\\x1F]).*$")) {
            errors.put("bhousenumber", "Please enter a valid house number");
            errors.put("status", "Failure");
            bhousenumber = "";
            bool = false;
        }

        if (dstreet.equals("") || !(dstreet.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("dstreet", "Please enter a valid street (alphanumeric (,)(.)(-)( ) only)");
            errors.put("status", "Failure");
            dstreet = "";
            bool = false;
        }


        if (bstreet.equals("") || !(bstreet.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("bstreet", "Please enter a valid street (alphanumeric (,)(.)(-)( ) only)");
            errors.put("status", "Failure");
            bstreet = "";
            bool = false;
        }


        if (dsubdivision.equals("") || !(dsubdivision.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("dsubdivision", "Please enter a valid subdivision (alphanumeric (,)(.)(-)( ) only)");
            errors.put("status", "Failure");
            dsubdivision = "";
            bool = false;
        }


        if (bsubdivision.equals("") || !(bsubdivision.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("bsubdivision", "Please enter a valid subdivision (alphanumeric (,)(.)(-)( ) only)");
            errors.put("status", "Failure");
            bsubdivision = "";
            bool = false;
        }

        if (dcity.equals("") || !(dcity.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("dcity", "Please enter a valid city (alphanumeric (,)(.)(-)( ) only)");
            errors.put("status", "Failure");
            dcity = "";
            bool = false;
        }

        if (bcity.equals("") || !(bcity.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("bcity", "Please enter a valid city (alphanumeric (,)(.)(-)( ) only)");
            errors.put("status", "Failure");
            bcity = "";
            bool = false;
        }

        if (dpostal.equals("") || !(dpostal.matches("(?=^.{4,}$)(?=[a-zA-Z0-9]*)(?!.*[\\W\\x00-\\x1F]).*$"))) {
            errors.put("dpostal", "Please enter a valid postal (alphanumeric only)");
            errors.put("status", "Failure");
            bcity = "";
            bool = false;
        }
        if (bpostal.equals("") || !(bpostal.matches("(?=^.{4,}$)(?=[a-zA-Z0-9]*)(?!.*[\\W\\x00-\\x1F]).*$"))) {
            errors.put("bpostal", "Please enter a valid postal (alphanumeric only)");
            errors.put("status", "Failure");
            bcity = "";
            bool = false;
        }

        if (!dbConnectorchk.checkcountry(dcountry)) {
            errors.put("dcountry", "Please enter a country");
            errors.put("status", "Failure");
            dcountry = "0";
            bool = false;
        }
        if (!dbConnectorchk.checkcountry(bcountry)) {
            errors.put("bcountry", "Please enter a country");
            errors.put("status", "Failure");
            bcountry = "0";
            bool = false;
        }
        firstName = WordUtils.capitalizeFully(firstName);
        if (firstName.equals("") || !(firstName.matches("^[a-zA-Z]+(([\\'\\,\\.\\-\\s][a-zA-Z])?[a-zA-Z]*)*$"))) {
            errors.put("firstName", "Please enter your first name (up to two words)");
            errors.put("status", "Failure");
            firstName = "";
            bool = false;

        }
        lastName = WordUtils.capitalizeFully(lastName);
        if (lastName.equals("") || !(firstName.matches("^[a-zA-Z]+(([\\'\\,\\.\\-\\s][a-zA-Z])?[a-zA-Z]*)*$"))) {
            errors.put("lastName", "Please enter your last name (up to two words)");
            errors.put("status", "Failure");
            lastName = "";
            bool = false;
        }

        if (middleInitial.equals("") || middleInitial.length() != 1 || !(middleInitial.matches("[A-Z]"))) {
            errors.put("middleInitial", "Please enter 1 uppercase Middle Initial");
            errors.put("status", "Failure");
            middleInitial = "";
            bool = false;
        }
        //^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$   credit card       
        if (creditcardnumber.equals("") || !(creditcardnumber.matches("^((4\\d{3})|(5[1-5]\\d{2})|(6011))-\\d{4}-\\d{4}-\\d{4}|3[4,7]\\d{13}$"))) {
            errors.put("creditcardnumber", "Please enter a valid credit card number (xxxx-xxxx-xxxx-xxxx)");
            errors.put("status", "Failure");
            creditcardnumber = "";
            bool = false;
        }
        //^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$ 


        if (creditcardexpiration.equals("") || !dbConnectorchk.isDate(creditcardexpiration)) {
            errors.put("creditcardexpiration", "Please enter a valid date yyyy-mm-dd)");
            errors.put("status", "Failure");
            creditcardexpiration = "";
            bool = false;
        }

        if (!(creditcardtype.equals("MasterCard") || creditcardtype.equals("Visa"))) {

            errors.put("creditcardtype", "Please enter a valid credit card type");
            errors.put("status", "Failure");
            creditcardtype = "0";
            bool = false;
        }


        if (bool == true) {
            DBConnector dbConnector = null;
            dbConnector = new DBConnector("localhost", 3306, "adder", "15mAdd3r", "bookaholic");
            System.out.println(username + "username");
            System.out.println(accounttype + "accounttype");
            //Create new user
            if (dbConnector.check_creditinfo(username)) {
                 dbConnector.updateCreditInfo(dhousenumber, dstreet, dsubdivision, dcity, dpostal, dcountry, bhousenumber, bstreet, bsubdivision, bcity, bpostal, bcountry, firstName, lastName, middleInitial, creditcardnumber, creditcardtype, creditcardexpiration, username, accounttype);
            } else {
                dbConnector.setCreditInfo(dhousenumber, dstreet, dsubdivision, dcity, dpostal, dcountry, bhousenumber, bstreet, bsubdivision, bcity, bpostal, bcountry, firstName, lastName, middleInitial, creditcardnumber, creditcardtype, creditcardexpiration, username, accounttype);
                //dbConnector.createProduct(title, addinfo, category, mtype, synopsis, price);
                username = "";
                accounttype = 0;
                dhousenumber = "";
                dstreet = "";
                dsubdivision = "";
                dcity = "";
                dpostal = "";
                dcountry = "";
                bhousenumber = "";
                bstreet = "";
                bsubdivision = "";
                bcity = "";
                bpostal = "";
                bcountry = "";
                firstName = "";
                lastName = "";
                middleInitial = "";
                creditcardnumber = "";
                creditcardtype = "";
                creditcardexpiration = "";

            }


            errors = new Hashtable();
            errors.put("status", "Success!  Info Changed");


        }

        return bool;
    }

    public int getAccounttype() {
        return accounttype;
    }

    public void setAccounttype(int accounttype) {
        this.accounttype = accounttype;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getErrorMsg(String s) {
        String errorMsg = (String) errors.get(s.trim());
        return (errorMsg == null) ? "" : errorMsg;
    }

    public void setErrors(String key, String msg) {
        errors.put(key, msg);
    }

    public String getBcity() {
        return bcity;
    }

    public void setBcity(String bcity) {
        this.bcity = bcity;
    }

    public String getBcountry() {
        return bcountry;
    }

    public void setBcountry(String bcountry) {
        this.bcountry = bcountry;
    }

    public String getBhousenumber() {
        return bhousenumber;
    }

    public void setBhousenumber(String bhousenumber) {
        this.bhousenumber = bhousenumber;
    }

    public String getBpostal() {
        return bpostal;
    }

    public void setBpostal(String bpostal) {
        this.bpostal = bpostal;
    }

    public String getBstreet() {
        return bstreet;
    }

    public void setBstreet(String bstreet) {
        this.bstreet = bstreet;
    }

    public String getBsubdivision() {
        return bsubdivision;
    }

    public void setBsubdivision(String bsubdivision) {
        this.bsubdivision = bsubdivision;
    }

    public String getCreditcardexpiration() {
        return creditcardexpiration;
    }

    public void setCreditcardexpiration(String creditcardexpiration) {
        this.creditcardexpiration = creditcardexpiration;
    }

    public String getCreditcardnumber() {
        return creditcardnumber;
    }

    public void setCreditcardnumber(String creditcardnumber) {
        this.creditcardnumber = creditcardnumber;
    }

    public String getCreditcardtype() {
        return creditcardtype;
    }

    public void setCreditcardtype(String creditcardtype) {
        this.creditcardtype = creditcardtype;
    }

    public String getDcity() {
        return dcity;
    }

    public void setDcity(String dcity) {
        this.dcity = dcity;
    }

    public String getDcountry() {
        return dcountry;
    }

    public void setDcountry(String dcountry) {
        this.dcountry = dcountry;
    }

    public String getDhousenumber() {
        return dhousenumber;
    }

    public void setDhousenumber(String dhousenumber) {
        this.dhousenumber = dhousenumber;
    }

    public String getDpostal() {
        return dpostal;
    }

    public void setDpostal(String dpostal) {
        this.dpostal = dpostal;
    }

    public String getDstreet() {
        return dstreet;
    }

    public void setDstreet(String dstreet) {
        this.dstreet = dstreet;
    }

    public String getDsubdivision() {
        return dsubdivision;
    }

    public void setDsubdivision(String dsubdivision) {
        this.dsubdivision = dsubdivision;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMiddleInitial() {
        return middleInitial;
    }

    public void setMiddleInitial(String middleInitial) {
        this.middleInitial = middleInitial;
    }
}
